from functools import reduce
from time import strftime, localtime
import datetime
import re
import requests
from First import models

def gengxinyonghujiapi():
    ulist = models.Yonghujiapi.objects.last()
    refresh_token = ulist.shujujieru['refresh_token']
    client_secret = ulist.baidufuwu['Secret Key']
    client_id = ulist.baidufuwu['API Key']
    request_url = "http://openapi.baidu.com/oauth/2.0/token?grant_type=refresh_token&refresh_token=" + refresh_token + "&client_id=" + client_id + "&client_secret=" + client_secret
    headers = {"content-type": "application/json"}
    response = requests.post(request_url, headers=headers)
    if "error" not in response.json():
        models.Yonghujiapi.objects.create(shijian=(datetime.datetime.now()).strftime('%Y-%m-%d %H:%M:%S'),yonghuyingyong=ulist.yonghuyingyong,shujujieru=response.json(),baidufuwu=ulist.baidufuwu)

def huoquyonghujiapi():
    ulist = models.Yonghujiapi.objects.last()
    accesstoken=ulist.shujujieru['access_token']
    appkey=ulist.yonghuyingyong['key']
    # appid=ulist.yonghuyingyong['aid']
    return (accesstoken,appkey)

def fdate():
    def get_real_time(delta_time, int11="null"):
        """
        获取几秒钟前、几分钟前、几小时前、几天前，几个月前、及几年前的具体时间
        :param delta_time 格式如：50秒前，10小时前，31天前，5个月前，8年前
        :return: 具体时间 %Y-%m-%d %H:%M:%S
        """
        # 获取表达式中的数字
        delta_num = int(re.findall("\d+", delta_time)[0])
        # 获取表达式中的文字
        delta_word = re.findall("\D+", delta_time)[0]
        units = {
            "分钟前": delta_num,
            "小时前": delta_num * 60,
            "天前": delta_num * 24 * 60,
            "个月前": int(delta_num * 365.0 / 12) * 24 * 60,
            "月前": int(delta_num * 365.0 / 12) * 24 * 60,
            "年前": delta_num * 365 * 24 * 60,
        }
        delta_time = datetime.timedelta(minutes=units[delta_word])
        return (datetime.datetime.now() - delta_time).strftime('%Y%m%d%H%M' + int11)
    str2221 = (int(strftime("%M", localtime())) % 5) + 5
    str111 = ("%d%s" % (str2221, "分钟前"))
    str222 = (int(strftime("%M", localtime())) % 5) + 1
    str11 = ("%d%s" % (str222, "分钟前"))
    start_date = get_real_time(str111, "00")
    end_date = get_real_time(str11, "59")
    return (start_date, end_date)

def zhdate():
    def get_real_time(delta_time, int11="null"):
        """
        获取几秒钟前、几分钟前、几小时前、几天前，几个月前、及几年前的具体时间
        :param delta_time 格式如：50秒前，10小时前，31天前，5个月前，8年前
        :return: 具体时间 %Y-%m-%d %H:%M:%S
        """
        # 获取表达式中的数字
        delta_num = int(re.findall("\d+", delta_time)[0])
        # 获取表达式中的文字
        delta_word = re.findall("\D+", delta_time)[0]
        units = {
            "分钟前": delta_num,
            "小时前": delta_num * 60,
            "天前": delta_num * 24 * 60,
            "个月前": int(delta_num * 365.0 / 12) * 24 * 60,
            "月前": int(delta_num * 365.0 / 12) * 24 * 60,
            "年前": delta_num * 365 * 24 * 60,
        }
        delta_time = datetime.timedelta(minutes=units[delta_word])
        return (datetime.datetime.now() - delta_time).strftime('%Y%m%d' + int11)
    start_date = get_real_time("7天前", "000000")
    end_date = get_real_time("1天前", "235959")
    return (start_date, end_date)

def get_qian_time(date_time,delta_time):
    """
    获取几秒钟前、几分钟前、几小时前、几天前，几个月前、及几年前的具体时间
    :param delta_time 格式如：50秒前，10小时前，31天前，5个月前，8年前
    :return: 具体时间 %Y-%m-%d %H:%M:%S
    """
    # 获取表达式中的数字
    delta_num = int(re.findall("\d+", delta_time)[0])
    # 获取表达式中的文字
    delta_word = re.findall("\D+", delta_time)[0]
    units = {
        "分钟前": delta_num,
        "小时前": delta_num * 60,
        "天前": delta_num * 24 * 60,
        "个月前": int(delta_num * 365.0 / 12) * 24 * 60,
        "月前": int(delta_num * 365.0 / 12) * 24 * 60,
        "年前": delta_num * 365 * 24 * 60,
    }
    delta_time = datetime.timedelta(minutes=units[delta_word])
    return (date_time - delta_time).strftime('%Y-%m-%d %H:%M:%S')

def get_hou_time(date_time,delta_time):
    """
    获取几秒钟后、几分钟后、几小时后、几天后，几个月后、及几年后的具体时间
    :param delta_time 格式如：50秒后，10小时后，31天后，5个月后，8年后
    :return: 具体时间 %Y-%m-%d %H:%M:%S
    """
    # 获取表达式中的数字
    delta_num = int(re.findall("\d+", delta_time)[0])
    # 获取表达式中的文字
    delta_word = re.findall("\D+", delta_time)[0]
    units = {
        "分钟后": delta_num,
        "小时后": delta_num * 60,
        "天后": delta_num * 24 * 60,
        "个月后": int(delta_num * 365.0 / 12) * 24 * 60,
        "月后": int(delta_num * 365.0 / 12) * 24 * 60,
        "年后": delta_num * 365 * 24 * 60,
    }
    delta_time = datetime.timedelta(minutes=units[delta_word])
    return (date_time + delta_time).strftime('%Y-%m-%d %H:%M:%S')

def list_dict_duplicate_removal(list_dict_data):
    run_function = lambda x, y: x if y in x else x + [y]
    return reduce(run_function, [[], ] + list_dict_data)